package flexo.graphic.entity;

import flexo.graphic.Geometry;
import flexo.graphic.Material;
import flexo.graphic.RenderQueue;
import flexo.scene.Node;

public abstract class Entity extends flexo.scene.Entity
{
	protected Geometry geom;
	protected Material mat;	
	
	public Entity()
	{
		super(Type.GRAPHIC);
	}
	
	/**
	 * Every class that inherits from this one, MUST call the init
	 * method once on it's constructor.
	 * @param geom The geometry of the entity
	 */
	protected void init(Geometry geom)
	{
		this.geom = geom;
		this.geom.grab();
	}
	
	public Material getMaterial()
	{
		return mat;
	}
	
	public void setMaterial(String name)
	{	
		flexo.graphic.GraphicSystem gs = (flexo.graphic.GraphicSystem)flexo.Kernel.getSystem(flexo.graphic.GraphicSystem.ID);
		Material material = gs.getMaterial(name);
		if (mat != material)
		{
			if (mat != null)
			{
				mat.remove();
			}
			
			mat = material;
			mat.grab();
		}
	}
	
	public void update(){}
	
	public void render()
	{
		update();
		
		if (mat != null)
		{
			mat.bind();
		}
		geom.render();
	}
	
	public void remove()
	{
		this.geom.remove();
		mat.remove();
	}
	
	public boolean attachTo(Node n)
	{
		boolean result = super.attachTo(n);
		if (result)
		{
			RenderQueue.update(n.getScene());
		}
		return result;
	}
	
	public void deattachFrom(Node n)
	{
		super.deattachFrom(n);
		RenderQueue.update(n.getScene());
	}
}